package com.lee.lease.web.app.service.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lee.lease.model.entity.ApartmentInfo;
import com.lee.lease.model.entity.LabelInfo;
import com.lee.lease.model.entity.ViewAppointment;
import com.lee.lease.model.enums.ItemType;
import com.lee.lease.web.app.mapper.*;
import com.lee.lease.web.app.service.ViewAppointmentService;
import com.lee.lease.web.app.vo.apartment.ApartmentItemVo;
import com.lee.lease.web.app.vo.apartment.AppointmentDetailVo;
import com.lee.lease.web.app.vo.appointment.ViewAppointmentVo;
import com.lee.lease.web.app.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {

    @Autowired
    private ViewAppointmentMapper mapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;


    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public List<ViewAppointmentVo> findListItem(Integer userId) {

        return mapper.findByUserId(userId);
    }

    @Override
    public AppointmentDetailVo getDetailById(String id) {
        //查询看房预约信息
        ViewAppointment viewAppointment = mapper.selectById(id);
        if (viewAppointment == null) {
            return null;
        }
        AppointmentDetailVo appointmentDetailVo = new AppointmentDetailVo();
        BeanUtils.copyProperties(viewAppointment, appointmentDetailVo);


        //查询对应的公寓
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(viewAppointment.getApartmentId());
        if (apartmentInfo != null) {
            ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
            BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
            //根据公寓id查询公寓标签
            List<LabelInfo> labelInfoList = labelInfoMapper.findByApartmentId(apartmentInfo.getId());
            //根据公寓id查询图片
            List<GraphVo> graphVoList = graphInfoMapper.findGraphVoList(ItemType.APARTMENT,apartmentInfo.getId());
            //根据公寓id查询最低租金
            BigDecimal minRent = BigDecimal.valueOf(roomInfoMapper.findMinRent(apartmentInfo.getId()));
            apartmentItemVo.setLabelInfoList(labelInfoList);
            apartmentItemVo.setGraphVoList(graphVoList);
            apartmentItemVo.setMinRent(minRent);

            //todo 组装
            appointmentDetailVo.setApartmentItemVo(apartmentItemVo);
        }


        return appointmentDetailVo;
    }
}




